import "dart:convert";
import 'dart:io';
import 'data.dart';
main() async {
  //注意单引号
  var requestServer=await HttpServer.bind('192.168.1.103', 8080);
  print('http服务启动起来');
  //处理请求
  await for (HttpRequest request in requestServer) {
    handleMessage(request);
  }
  //测试http://192.168.1.103:8080/
}
void handleMessage(HttpRequest request){
  try{
    if(request.method=='GET'){
      handleGET(request);
    }else if(request.method=='POST'){
       handlePOST(request);
    }

  }catch(e){
    print('捕获了一个异常:$e');
  }
}
/**
 * 处理get请求
 */
void handleGET(HttpRequest request){
  var action=request.uri.queryParameters['action'];
  var page=request.uri.queryParameters['page'];
  if(action=='getProducts'){
    print("第${page}页数据");
    request.response..write(json.encode(products))..close();
  }else if(action=='getNews'){
    request.response..write(json.encode(news))..close();
  }
}
/**
 * 处理post请求
 */
void handlePOST(HttpRequest request){
  
}